package com.djsearch.adapter;

import com.djsearch.R;
import com.djsearch.data.CommunicationConstants;
import com.djsearch.ui.GroupShopActivity;
import com.djsearch.ui.JingDianActivity;
import com.djsearch.ui.LvYouActivity;
import com.djsearch.ui.MovieActivity;
import com.djsearch.ui.SearchAppActivity;
import com.djsearch.ui.TaoGouActivity;
import com.djsearch.ui.WanNengSearchActivity;
import com.djsearch.ui.WebViewActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 主界面中栏目布局适配器（gridVIew控件）
 * @author chenyang2
 *
 */

public class MainGridViewAdapter extends BaseAdapter {
	
	private Context mContext;
	private Integer[] mImageIds={
			R.drawable.tuangou_selector,
			R.drawable.lvyou_selector,
			R.drawable.taogou_selector,
			R.drawable.search_selector,
			R.drawable.jingdian_seletor,
			R.drawable.movie_selector
			
	};
	public MainGridViewAdapter(Context context){
		mContext = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mImageIds.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = LayoutInflater.from(mContext).inflate(R.layout.main_gridview_item, null);
		ImageView imageview = (ImageView)view.findViewById(R.id.ivAppIcon);
		imageview.setImageResource(mImageIds[position]);
		imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
		imageview.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent;
				
				if (position == 0) {
					startActivity(GroupShopActivity.class);
				}
				if (position == 1) {
					startActivity(LvYouActivity.class);
				}
				if (position == 2) {
					startActivity(TaoGouActivity.class);
				}
				if (position == 3) {
//					startActivity(SearchAppActivity.class);
//					Intent intentUrl=new Intent();
//					intentUrl.setClass(mContext, WebViewActivity.class);
//			        Bundle bundle = new Bundle();
//			        bundle.putString("url", CommunicationConstants.appUrl);
//			        bundle.putString("whichBuyType", "taoGou");
//			        intentUrl.putExtras(bundle);
////			        intent.putExtra("url", groupBuyGoods.getShopUrl());
//			        mContext.startActivity(intentUrl);
//			        ((Activity) mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			        
			        Intent intentUrl= new Intent(); 

					if(intentUrl.setAction("android.intent.action.VIEW")==null) {
						return;
				    }
				    Uri content_url = Uri.parse(CommunicationConstants.appUrl);   
				    intentUrl.setData(content_url);  
				    mContext.startActivity(intentUrl);
			        ((Activity) mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

			        
			        
			        
			        
				}
				if (position == 4) {
					startActivity(JingDianActivity.class);
				}
				if (position == 5) {
					startActivity(MovieActivity.class);
				}
				
			}
		});
		return view;
	}

	private void startActivity(Class<?> toActivity){
		Intent intent;
		intent=new Intent(mContext, toActivity);
		mContext.startActivity(intent);
	    ((Activity) mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
//	    ((Activity) mContext).finish();
	}
	
}
